/*!
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * 项目自定义的公共JavaScript，可覆盖jeesite.js里的方法
 */

var LayWin = {};
var vLayuiUtil = {
    openMinWindow: function (id, url, title, area) {
        $.ajax({
            type: "get",
            url: url,
            date: {},
            complete: function (xhr, textStatus) {
                if (!title) {
                    title = '提示';
                }
                if (!area) {
                    area = ['450px', '350px'];
                }
                var layui = {
                    type: 1,
                    title: title,
                    area: area,
                    id: id,
                    content: xhr.responseText
                };

                vLayuiUtil.createLayerWindow(id, layui)
            }
        });
    },
    openLayerWindow: function (id, url, title, area) {
        $.ajax({
            type: "get",
            url: url,
            date: {},
            complete: function (xhr, textStatus) {
                if (!title) {
                    title = '提示';
                }
                if (!area) {
                    area = ['600px', '450px'];
                }
                var layui = {
                    type: 1,
                    title: title,
                    maxmin: true,
                    area: area,
                    id: id,
                    content: xhr.responseText
                };

                vLayuiUtil.createLayerWindow(id, layui)
            }
        });
    },
    openLayerMaxWindow: function (id, url, title) {
        $.ajax({
            type: "get",
            url: url,
            date: {},
            complete: function (xhr, textStatus) {
                if (!title) {
                    title = '提示';
                }
                var layui = {
                    type: 1,
                    title: title,
                    maxmin: true,
                    area: ['98%', '98%'],
                    id: id,
                    content: xhr.responseText
                };

                vLayuiUtil.createLayerWindow(id, layui)
            }
        });
    },
    openLayerIframWindow: function (id, url, title, area) {
        if (!title) {
            title = '提示';
        }
        if (!area) {
            area = ['600px', '450px'];
        }
        var layui = {
            type: 2,
            title: title,
            maxmin: true,
            area: area,
            id: id,
            content: url
        };
        vLayuiUtil.createLayerWindow(id, layui)
    },
    openLayerIframMaxWindow: function (id, url, title) {
        if (!title) {
            title = '提示';
        }
        var layui = {
            type: 2,
            title: title,
            maxmin: true,
            area: ['98%', '98%'],
            id: id,
            content: url
        };
        vLayuiUtil.createLayerWindow(id, layui)
    },
    createLayerWindow: function (id, setting) {
        LayWin[id] = layer.open(setting);
    }
};

function showModal(modal) {
    $("#" + modal).modal('show');
}

function closeModal(modal) {
    $("#" + modal).modal('hide');
}

function magicSuggestValue(value) {
    var array = [];
    if (value !== '' && value != null && value !== undefined) {
        array.push(value);
    }
    return array;
}

var validateOption;
function validateMagicSuggest(option) {
    if(option==null){
        return false;
    }
    validateOption = option;
    var validateFlag = true;
    for (var domeKey in option) {
        var name = "" + domeKey + "[]";
        var value = $("input[name='" + name + "']").val();
        var formGroup = document.getElementById(domeKey).parentElement.parentElement;
        if (value == null || value == '' || value == undefined) {
            if (formGroup.classList.contains('has-error') === false) {
                formGroup.classList.add("has-error");
            }
            validateFlag = false;
        } else {
            formGroup.classList.remove('has-error');
        }
    }
    return validateFlag;
}

function validateMagicReload() {
    validateMagicSuggest(validateOption);
}